1 /************************************************************
2 * Copyright *
3 * Portions of this software are Copyright (c) 1993 - 2002, *
4 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
5 * - http://www.nevrona.com/Indy/ *
6 ************************************************************/
7 package org.indy.udp;
8
9 import java.io.IOException;
10
11 import java.net.DatagramPacket;
12 import java.net.DatagramSocket;
13 import java.net.InetAddress;
14 import java.net.SocketException;
15 import java.net.UnknownHostException;
16
17 import org.indy.IndyComponent;
18
19 import org.indy.io.IndyIOException;
20 import org.indy.io.IndyUnknownHostException;
21
22 import org.indy.util.IndyUtilities;
23
24
25 /***
26 * NB, Broadcast is ALWAYS enabled in Java UDP sockets,
27 * so there's no point including the property that appears in
28 * the Delphi version.
29 */
30 public abstract class IdUDPBase extends IndyComponent {
31 /*** DOCUMENT ME! */
32 public static final int UDP_BUFFERSIZE = 8192;
33 private boolean active = false;
34 private DatagramSocket binding;
35 private int receiveTimeout = 0;
36 private InetAddress host;
37 private int port;
38
39 /***
40 * Creates a new IdUDPBase object.
41 */
42 public IdUDPBase() {
43 super();
44
45 //binding.setReceiveBufferSize(UDP_BUFFERSIZE);
46 }
47
48 /***
49 * DOCUMENT ME!
50 *
51 * @return DOCUMENT ME!
52 */
53 public boolean getActive() {
54 return active;
55 }
56
57 /***
58 * DOCUMENT ME!
59 *
60 * @param value DOCUMENT ME!
61 *
62 * @throws IndyIOException DOCUMENT ME!
63 */
64 public void setActive(boolean value) throws IndyIOException {
65 try {
66 if (value != active) {
67 if (value) {
68 binding = new DatagramSocket();
69 }
70 else {
71 binding.close();
72 binding = null;
73 }
74 }
75
76 active = value;
77 }
78 catch (SocketException se) {
79 throw new IndyIOException(se);
80 }
81 }
82
83 /***
84 * DOCUMENT ME!
85 *
86 * @param b DOCUMENT ME!
87 * @param off DOCUMENT ME!
88 * @param len DOCUMENT ME!
89 * @param peer DOCUMENT ME!
90 * @param peerPort DOCUMENT ME!
91 * @param timeout DOCUMENT ME!
92 *
93 * @return DOCUMENT ME!
94 *
95 * @throws IndyIOException DOCUMENT ME!
96 */
97 protected final int receiveBuffer(byte[] b, int off, int len,
98 InetAddress peer, int peerPort, int timeout)
99 throws IndyIOException {
100 if (active) {
101 if (timeout == IndyUtilities.getDefaultTimeout()) {
102 timeout = receiveTimeout;
103 }
104
105 DatagramPacket dp = new DatagramPacket(b, off, len, peer, peerPort);
106
107 try {
108 binding.setSoTimeout(timeout);
109 binding.receive(dp);
110
111 return dp.getLength();
112 }
113 catch (IOException ioe) {
114 throw new IndyIOException(ioe);
115 }
116 }
117 else {
118 return 0;
119 }
120 }
121
122 /***
123 * DOCUMENT ME!
124 *
125 * @param b DOCUMENT ME!
126 * @param off DOCUMENT ME!
127 * @param len DOCUMENT ME!
128 * @param peerIP DOCUMENT ME!
129 * @param peerPort DOCUMENT ME!
130 * @param timeout DOCUMENT ME!
131 *
132 * @return DOCUMENT ME!
133 *
134 * @throws IndyIOException DOCUMENT ME!
135 * @throws IndyUnknownHostException DOCUMENT ME!
136 */
137 public int receiveBuffer(byte[] b, int off, int len, String peerIP,
138 int peerPort, int timeout) throws IndyIOException,
139 IndyUnknownHostException {
140 try {
141 InetAddress ia = InetAddress.getByName(peerIP);
142
143 return receiveBuffer(b, off, len, ia, peerPort, timeout);
144 }
145 catch (UnknownHostException uhe) {
146 throw new IndyUnknownHostException(uhe);
147 }
148 }
149
150 /***
151 * DOCUMENT ME!
152 *
153 * @param b DOCUMENT ME!
154 * @param peerIP DOCUMENT ME!
155 * @param peerPort DOCUMENT ME!
156 * @param timeout DOCUMENT ME!
157 *
158 * @return DOCUMENT ME!
159 *
160 * @throws IndyIOException DOCUMENT ME!
161 * @throws IndyUnknownHostException DOCUMENT ME!
162 */
163 public int receiveBuffer(byte[] b, String peerIP, int peerPort, int timeout)
164 throws IndyIOException, IndyUnknownHostException {
165 return receiveBuffer(b, 0, 0, peerIP, peerPort);
166 }
167
168 /***
169 * DOCUMENT ME!
170 *
171 * @param b DOCUMENT ME!
172 * @param off DOCUMENT ME!
173 * @param len DOCUMENT ME!
174 * @param peerIP DOCUMENT ME!
175 * @param peerPort DOCUMENT ME!
176 *
177 * @return DOCUMENT ME!
178 *
179 * @throws IndyIOException DOCUMENT ME!
180 * @throws IndyUnknownHostException DOCUMENT ME!
181 */
182 public int receiveBuffer(byte[] b, int off, int len, String peerIP,
183 int peerPort) throws IndyIOException,
184 IndyUnknownHostException {
185 return receiveBuffer(b, off, len, peerIP, peerPort,
186 IndyUtilities.getDefaultTimeout());
187 }
188
189 /***
190 * DOCUMENT ME!
191 *
192 * @param b DOCUMENT ME!
193 * @param peerIP DOCUMENT ME!
194 * @param peerPort DOCUMENT ME!
195 *
196 * @return DOCUMENT ME!
197 *
198 * @throws IndyIOException DOCUMENT ME!
199 * @throws IndyUnknownHostException DOCUMENT ME!
200 */
201 public int receiveBuffer(byte[] b, String peerIP, int peerPort)
202 throws IndyIOException, IndyUnknownHostException {
203 return receiveBuffer(b, 0, 0, peerIP, peerPort);
204 }
205
206 /***
207 * DOCUMENT ME!
208 *
209 * @param b DOCUMENT ME!
210 * @param off DOCUMENT ME!
211 * @param len DOCUMENT ME!
212 * @param timeout DOCUMENT ME!
213 *
214 * @return DOCUMENT ME!
215 *
216 * @throws IndyIOException DOCUMENT ME!
217 * @throws IllegalStateException DOCUMENT ME!
218 */
219 public int receiveBuffer(byte[] b, int off, int len, int timeout)
220 throws IndyIOException {
221 if ((host == null) || (port == 0)) {
222 throw new IllegalStateException(
223 "Host and Port must be set before calling this method");
224 }
225
226 return receiveBuffer(b, off, len, host, port, timeout);
227 }
228
229 /***
230 * DOCUMENT ME!
231 *
232 * @param b DOCUMENT ME!
233 * @param timeout DOCUMENT ME!
234 *
235 * @return DOCUMENT ME!
236 *
237 * @throws IndyIOException DOCUMENT ME!
238 */
239 public int receiveBuffer(byte[] b, int timeout) throws IndyIOException {
240 return receiveBuffer(b, 0, 0, timeout);
241 }
242
243 /***
244 * DOCUMENT ME!
245 *
246 * @param b DOCUMENT ME!
247 * @param off DOCUMENT ME!
248 * @param len DOCUMENT ME!
249 *
250 * @return DOCUMENT ME!
251 *
252 * @throws IndyIOException DOCUMENT ME!
253 * @throws IllegalStateException DOCUMENT ME!
254 */
255 public int receiveBuffer(byte[] b, int off, int len)
256 throws IndyIOException {
257 if ((host == null) || (port == 0)) {
258 throw new IllegalStateException(
259 "Host and Port must be set before calling this method");
260 }
261
262 return receiveBuffer(b, off, len, host, port,
263 IndyUtilities.getDefaultTimeout());
264 }
265
266 /***
267 * DOCUMENT ME!
268 *
269 * @param b DOCUMENT ME!
270 *
271 * @return DOCUMENT ME!
272 *
273 * @throws IndyIOException DOCUMENT ME!
274 */
275 public int receiveBuffer(byte[] b) throws IndyIOException {
276 return receiveBuffer(b, 0, 0, IndyUtilities.getDefaultTimeout());
277 }
278
279 /***
280 * DOCUMENT ME!
281 *
282 * @param receiveTimeout DOCUMENT ME!
283 */
284 public void setReceiveTimeout(int receiveTimeout) {
285 this.receiveTimeout = receiveTimeout;
286 }
287
288 /***
289 * DOCUMENT ME!
290 *
291 * @return DOCUMENT ME!
292 */
293 public int getReceiveTimeout() {
294 return receiveTimeout;
295 }
296
297 /***
298 * DOCUMENT ME!
299 *
300 * @return DOCUMENT ME!
301 */
302 public String getHost() {
303 return host.getHostAddress();
304 }
305
306 /***
307 * DOCUMENT ME!
308 *
309 * @param newHost DOCUMENT ME!
310 *
311 * @throws IndyUnknownHostException DOCUMENT ME!
312 */
313 public void setHost(String newHost) throws IndyUnknownHostException {
314 try {
315 host = InetAddress.getByName(newHost);
316 }
317 catch (UnknownHostException uhe) {
318 throw new IndyUnknownHostException(uhe);
319 }
320 }
321
322 /***
323 * DOCUMENT ME!
324 *
325 * @return DOCUMENT ME!
326 */
327 public int getPort() {
328 return port;
329 }
330
331 /***
332 * DOCUMENT ME!
333 *
334 * @param port DOCUMENT ME!
335 */
336 public void setPort(int port) {
337 this.port = port;
338 }
339 }
This page was automatically generated by Maven